package com.songlanyun.modules.offlinegoods.controller.api;

import java.math.BigDecimal;
import java.util.Map;

import javax.validation.constraints.NotNull;

import cn.hutool.core.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.common.validator.group.AddGroup;
import com.songlanyun.common.validator.group.UpdateGroup;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.offlinegoods.entity.OfflineGoods;
import com.songlanyun.modules.offlinegoods.model.dto.OfflineGoodsDTO;
import com.songlanyun.modules.offlinegoods.service.OfflineGoodsService;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;

/**
 * 线下商品
 *
 * @author lijiahe
 * @date 2023-02-11
 */
@RestController
@RequestMapping("api/offline/goods")
public class OfflineGoodsApiController extends AbstractApiController {

    @Autowired
    private OfflineGoodsService offlineGoodsService;


    @GetMapping("/page")
    public R page(@RequestParam Map<String, Object> params) {
        return R.ok().put("page", offlineGoodsService.queryPage(params));
    }


    /**
     * 工作台下线商品分页
     *
     * @param params
     * @return
     */
    @GetMapping("/my_page")
    public R myPage(@RequestParam Map<String, Object> params) {
        Long userId = getUserId(true);
        params.put("userId", userId);
        params.put("shopId", getShopByUserId(userId, true, true));
        return R.ok().put("page", offlineGoodsService.queryPage(params));
    }


    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        OfflineGoods offlineGoods = offlineGoodsService.loadOfflineGoodsById(id, true);
        return R.ok().put("data", offlineGoods);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody OfflineGoodsDTO dto) {
        ValidatorUtils.validateEntity(dto, AddGroup.class);
        Long userId = getUserId(true);
        Long shopId = getShopByUserId(userId, true, true);
        dto.setCreateBy(userId);
        dto.setShopId(shopId);
        offlineGoodsService.saveDto(dto);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody OfflineGoodsDTO dto) {
        ValidatorUtils.validateEntity(dto, UpdateGroup.class);
        Long shopId = getShopByUserId(getUserId(true), true, true);
        offlineGoodsService.updateDto(shopId, dto);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        Long shopId = getShopByUserId(getUserId(true), true, true);
        offlineGoodsService.deleteWithValidById(id, shopId, true);
        return R.ok();
    }

    /**
     * 商品上下架
     */
    @PutMapping("/upOrOffShelf/{id}")
    public R upOrOffShelf(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        Long shopId = getShopByUserId(getUserId(true), true, true);
        offlineGoodsService.upOrOffShelf(id, shopId);
        return R.ok();
    }

}
